!############################# Change Log ##################################
! 3.1.0
!
!###########################################################################
!  Copyright (C)  1990, 1995, 1999, 2000, 2003, 2004 - All Rights Reserved
!  Brazilian Regional Atmospheric Modeling System - BRAMS
!###########################################################################

module mem_stilt
 
 use grid_dims
 use mem_turb, only : idiffk
 
 type stilt_vars
   real, pointer, dimension(:,:,:) :: &
                            thvlast,afxu, afxv, afxw &
                           ,ltscale, sigw, ltscaleb, sigwb &
                           ,tkepb, afxub, afxvb, afxwb &
                           ,cfxup1, cfxdn1, dfxup1, efxup1 &
                           ,dfxdn1, efxdn1, cfxup2, dfxup2 &
                           ,efxup2                         & 
!-srf : for the true air density
                           ,dnp

   real, pointer, dimension(:,:) :: pblhgt,lmo
 end type

 type (stilt_vars), allocatable :: stilt_g(:), stiltm_g(:)
 integer                        :: iexev,imassflx
contains

 subroutine alloc_stilt(stilt,n1,n2,n3,ng)
   implicit none
   type (stilt_vars) :: stilt
   integer, intent(in) :: n1, n2, n3, ng
 
! Allocate arrays based on options (if necessary)
   if (iexev == 2) then
     allocate (stilt%thvlast(n1,n2,n3))
!-srf : for the true air density
     allocate (stilt%dnp(n1,n2,n3))
   endif
   
   
   if (idiffk(ng) == 7) then
     allocate (stilt%ltscale(n1,n2,n3))
     allocate (stilt%sigw(n1,n2,n3))
     allocate (stilt%pblhgt(n2,n3))
     allocate (stilt%lmo(n2,n3))
   end if
   if (imassflx == 1) then
     allocate (stilt%afxu(n1,n2,n3))
     allocate (stilt%afxv(n1,n2,n3))
     allocate (stilt%afxw(n1,n2,n3))
     allocate (stilt%afxub(n1,n2,n3))
     allocate (stilt%afxvb(n1,n2,n3))
     allocate (stilt%afxwb(n1,n2,n3))
     allocate (stilt%cfxup1(n1,n2,n3))
     allocate (stilt%cfxdn1(n1,n2,n3))
     allocate (stilt%dfxup1(n1,n2,n3))
     allocate (stilt%efxup1(n1,n2,n3))
     allocate (stilt%dfxdn1(n1,n2,n3))
     allocate (stilt%efxdn1(n1,n2,n3))
     allocate (stilt%cfxup2(n1,n2,n3))
     allocate (stilt%dfxup2(n1,n2,n3))
     allocate (stilt%efxup2(n1,n2,n3))
     if (idiffk(ng) /= 2 .and. idiffk(ng) /= 3) then
       allocate (stilt%tkepb(n1,n2,n3))
     end if
     if (idiffk(ng) == 7) then
       allocate (stilt%ltscaleb(n1,n2,n3))
       allocate (stilt%sigwb(n1,n2,n3))
     end if
   end if
   return
 end subroutine alloc_stilt
 
 
 subroutine nullify_stilt(stilt)
   implicit none
   type (stilt_vars)   :: stilt

   if (associated(stilt%thvlast ))  nullify (stilt%thvlast )
!-srf : for the true air density
   if (associated(stilt%dnp     ))  nullify (stilt%dnp     )

   if (associated(stilt%ltscale ))  nullify (stilt%ltscale )
   if (associated(stilt%sigw    ))  nullify (stilt%sigw    )
   if (associated(stilt%pblhgt  ))  nullify (stilt%pblhgt  )
   if (associated(stilt%lmo     ))  nullify (stilt%lmo     )
   if (associated(stilt%afxu    ))  nullify (stilt%afxu    )
   if (associated(stilt%afxv    ))  nullify (stilt%afxv    )
   if (associated(stilt%afxw    ))  nullify (stilt%afxw    )
   if (associated(stilt%ltscaleb))  nullify (stilt%ltscaleb)
   if (associated(stilt%sigwb   ))  nullify (stilt%sigwb   )
   if (associated(stilt%tkepb   ))  nullify (stilt%tkepb   ) 
   if (associated(stilt%afxub   ))  nullify (stilt%afxub   )
   if (associated(stilt%afxvb   ))  nullify (stilt%afxvb   )
   if (associated(stilt%afxwb   ))  nullify (stilt%afxwb   )
   if (associated(stilt%cfxup1  ))  nullify (stilt%cfxup1  )
   if (associated(stilt%cfxdn1  ))  nullify (stilt%cfxdn1  )
   if (associated(stilt%dfxup1  ))  nullify (stilt%dfxup1  )
   if (associated(stilt%efxup1  ))  nullify (stilt%efxup1  )
   if (associated(stilt%dfxdn1  ))  nullify (stilt%dfxdn1  )
   if (associated(stilt%efxdn1  ))  nullify (stilt%efxdn1  )
   if (associated(stilt%cfxup2  ))  nullify (stilt%cfxup2  )
   if (associated(stilt%dfxup2  ))  nullify (stilt%dfxup2  )
   if (associated(stilt%efxup2  ))  nullify (stilt%efxup2  )
   return
 end subroutine

 subroutine dealloc_stilt(stilt)
   implicit none
   type (stilt_vars)   :: stilt

   if (associated(stilt%thvlast ))  deallocate (stilt%thvlast )
!-srf : for the true air density
   if (associated(stilt%dnp     ))  deallocate (stilt%dnp     )

   if (associated(stilt%ltscale ))  deallocate (stilt%ltscale )
   if (associated(stilt%sigw    ))  deallocate (stilt%sigw    )
   if (associated(stilt%pblhgt  ))  deallocate (stilt%pblhgt  )
   if (associated(stilt%lmo     ))  deallocate (stilt%lmo     )
   if (associated(stilt%afxu    ))  deallocate (stilt%afxu    )
   if (associated(stilt%afxv    ))  deallocate (stilt%afxv    )
   if (associated(stilt%afxw    ))  deallocate (stilt%afxw    )
   if (associated(stilt%ltscaleb))  deallocate (stilt%ltscaleb)
   if (associated(stilt%sigwb   ))  deallocate (stilt%sigwb   )
   if (associated(stilt%tkepb   ))  deallocate (stilt%tkepb   )
   if (associated(stilt%afxub   ))  deallocate (stilt%afxub   )
   if (associated(stilt%afxvb   ))  deallocate (stilt%afxvb   )
   if (associated(stilt%afxwb   ))  deallocate (stilt%afxwb   )
   if (associated(stilt%cfxup1  ))  deallocate (stilt%cfxup1  )
   if (associated(stilt%cfxdn1  ))  deallocate (stilt%cfxdn1  )
   if (associated(stilt%dfxup1  ))  deallocate (stilt%dfxup1  )
   if (associated(stilt%efxup1  ))  deallocate (stilt%efxup1  )
   if (associated(stilt%dfxdn1  ))  deallocate (stilt%dfxdn1  )
   if (associated(stilt%efxdn1  ))  deallocate (stilt%efxdn1  )
   if (associated(stilt%cfxup2  ))  deallocate (stilt%cfxup2  )
   if (associated(stilt%dfxup2  ))  deallocate (stilt%dfxup2  )
   if (associated(stilt%efxup2  ))  deallocate (stilt%efxup2  )
   return
 end subroutine 

 subroutine filltab_stilt(stilt,stiltm,imean,n1,n2,n3,ng)
 
   use var_tables
   
   implicit none
   type (stilt_vars)   :: stilt,stiltm
   integer             :: imean, n1, n2, n3, ng
   integer             :: npts
   real, pointer       :: var, varm

   npts=n1*n2*n3

   if (associated(stilt%thvlast)) &
      call vtables2 (stilt%thvlast(1,1,1),stiltm%thvlast(1,1,1) &
                 ,ng, npts, imean, &
                 'THVLAST :3:hist:mpti:mpt3:mpt1')
 
!-srf : for the true air density
   if (associated(stilt%dnp)) &
      call vtables2 (stilt%dnp(1,1,1),stiltm%dnp(1,1,1) &
                 ,ng, npts, imean, &
                 'DNP :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%ltscale)) &
      call vtables2 (stilt%ltscale(1,1,1),stiltm%ltscale(1,1,1) &
                 ,ng, npts, imean, &
                 'TL :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%sigw)) &
      call vtables2 (stilt%sigw(1,1,1),stiltm%sigw(1,1,1) &
                 ,ng, npts, imean, &
                 'SIGW :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%afxu)) &
      call vtables2 (stilt%afxu(1,1,1),stiltm%afxu(1,1,1) &
                 ,ng, npts, imean, &
                 'AFXU :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%afxv)) &
      call vtables2 (stilt%afxv(1,1,1),stiltm%afxv(1,1,1) &
                 ,ng, npts, imean, &
                 'AFXV :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%afxw)) &
      call vtables2 (stilt%afxw(1,1,1),stiltm%afxw(1,1,1) &
                 ,ng, npts, imean, &
                 'AFXW :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%ltscaleb)) &
      call vtables2 (stilt%ltscaleb(1,1,1),stiltm%ltscaleb(1,1,1) &
                 ,ng, npts, imean, &
                 'TLB :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%sigwb)) &
      call vtables2 (stilt%sigwb(1,1,1),stiltm%sigwb(1,1,1) &
                 ,ng, npts, imean, &
                 'SIGWB :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%tkepb)) &
      call vtables2 (stilt%tkepb(1,1,1),stiltm%tkepb(1,1,1) &
                 ,ng, npts, imean, &
                 'TKEPB :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%afxub)) &
      call vtables2 (stilt%afxub(1,1,1),stiltm%afxub(1,1,1) &
                 ,ng, npts, imean, &
                 'AFXUB :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%afxvb)) &
      call vtables2 (stilt%afxvb(1,1,1),stiltm%afxvb(1,1,1) &
                 ,ng, npts, imean, &
                 'AFXVB :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%afxwb)) &
      call vtables2 (stilt%afxwb(1,1,1),stiltm%afxwb(1,1,1) &
                 ,ng, npts, imean, &
                 'AFXWB :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%cfxup1)) &
      call vtables2 (stilt%cfxup1(1,1,1),stiltm%cfxup1(1,1,1) &
                 ,ng, npts, imean, &
                 'CFXUP1 :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%cfxdn1)) &
      call vtables2 (stilt%cfxdn1(1,1,1),stiltm%cfxdn1(1,1,1) &
                 ,ng, npts, imean, &
                 'CFXDN1 :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%dfxup1)) &
      call vtables2 (stilt%dfxup1(1,1,1),stiltm%dfxup1(1,1,1) &
                 ,ng, npts, imean, &
                 'DFXUP1 :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%efxup1)) &
      call vtables2 (stilt%efxup1(1,1,1),stiltm%efxup1(1,1,1) &
                 ,ng, npts, imean, &
                 'EFXUP1 :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%dfxdn1)) &
      call vtables2 (stilt%dfxdn1(1,1,1),stiltm%dfxdn1(1,1,1) &
                 ,ng, npts, imean, &
                 'DFXDN1 :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%efxdn1)) &
      call vtables2 (stilt%efxdn1(1,1,1),stiltm%efxdn1(1,1,1) &
                 ,ng, npts, imean, &
                 'EFXDN1 :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%cfxup2)) &
      call vtables2 (stilt%cfxup2(1,1,1),stiltm%cfxup2(1,1,1) &
                 ,ng, npts, imean, &
                 'CFXUP2 :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%dfxup2)) &
      call vtables2 (stilt%dfxup2(1,1,1),stiltm%dfxup2(1,1,1) &
                 ,ng, npts, imean, &
                 'DFXUP2 :3:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%efxup2)) &
      call vtables2 (stilt%efxup2(1,1,1),stiltm%efxup2(1,1,1) &
                 ,ng, npts, imean, &
                 'EFXUP2 :3:hist:anal:mpti:mpt3:mpt1')

   npts=n2*n3
   if (associated(stilt%pblhgt)) &
      call vtables2 (stilt%pblhgt(1,1),stiltm%pblhgt(1,1) &
                 ,ng, npts, imean, &
                 'PBLHGT :2:hist:anal:mpti:mpt3:mpt1')

   if (associated(stilt%lmo)) &
      call vtables2 (stilt%lmo(1,1),stiltm%lmo(1,1) &
                 ,ng, npts, imean, &
                 'LMO    :2:hist:anal:mpti:mpt3:mpt1')

   return
 end subroutine filltab_stilt

end module mem_stilt
